﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutoBuyWinform.entity;
using System.Collections;

namespace AutoBuyWinform
{
    public class ProductConditions
    {
        private ArrayList ps;

        public ProductConditions()
        {
        }

        public ProductCondition getProductCondition(int index)
        {
            if (index < ps.Count)
                return (ProductCondition)ps[index];
            else
                return null;
        }

        /// <summary>
        /// 加载商品条件
        /// </summary>
        /// <param name="grid"></param>
        /// <returns>成功标志</returns>
        public bool readGrid(DataGridView grid)
        {
            ps = new ArrayList();
            for (int i = 0; i < grid.Rows.Count - 1; i++)
            {
                DataGridViewRow row = grid.Rows[i];
                ProductCondition p = new ProductCondition();
                Double tmpD;

                String fl = (String)row.Cells[grid.Columns["fl"].Index].Value;
                if (fl != null)
                {
                    p.flmc = fl.Split(':')[0];
                    p.fl = fl.Split(':')[1];
                }
                else
                {
                    MessageBox.Show("单元格[" + row.Index + "," + grid.Columns["fl"].Index + "]的数据有错误");
                    return false;
                }

                String pm = (String)row.Cells[grid.Columns["pm"].Index].Value;
                if (pm != null)
                {
                    p.pm = pm;
                }
                else
                {
                    MessageBox.Show("单元格[" + row.Index + "," + grid.Columns["pm"].Index + "]的数据有错误");
                    return false;
                }

                String ph = (String)row.Cells[grid.Columns["ph"].Index].Value;
                if (ph != null)
                {
                    p.ph = ph;
                }
                else
                {
                    MessageBox.Show("单元格[" + row.Index + "," + grid.Columns["ph"].Index + "]的数据有错误");
                    return false;
                }

                String hd_min = (String)row.Cells[grid.Columns["hd_min"].Index].Value;
                if (hd_min != null && Double.TryParse(hd_min, out tmpD))
                {
                    p.hd_min = Double.Parse(hd_min);
                }
                else
                {
                    MessageBox.Show("单元格[" + row.Index + "," + grid.Columns["hd_min"].Index + "]的数据有错误");
                    return false;
                }

                String hd_max = (String)row.Cells[grid.Columns["hd_max"].Index].Value;
                if (hd_max != null && Double.TryParse(hd_max, out tmpD))
                {
                    p.hd_max = Double.Parse(hd_max);
                }
                else
                {
                    MessageBox.Show("单元格[" + row.Index + "," + grid.Columns["hd_max"].Index + "]的数据有错误");
                    return false;
                }

                String kd_min = (String)row.Cells[grid.Columns["kd_min"].Index].Value;
                if (kd_min != null && Double.TryParse(kd_min, out tmpD))
                {
                    p.kd_min = Double.Parse(kd_min);
                }
                else
                {
                    MessageBox.Show("单元格[" + row.Index + "," + grid.Columns["kd_min"].Index + "]的数据有错误");
                    return false;
                }

                String kd_max = (String)row.Cells[grid.Columns["kd_max"].Index].Value;
                if (kd_max != null && Double.TryParse(kd_max, out tmpD))
                {
                    p.kd_max = Double.Parse(kd_max);
                }
                else
                {
                    MessageBox.Show("单元格[" + row.Index + "," + grid.Columns["kd_max"].Index + "]的数据有错误");
                    return false;
                }

                String zl_min = (String)row.Cells[grid.Columns["zl_min"].Index].Value;
                if (zl_min != null && Double.TryParse(zl_min, out tmpD))
                {
                    p.zl_min = Double.Parse(zl_min);
                }
                else
                {
                    MessageBox.Show("单元格[" + row.Index + "," + grid.Columns["zl_min"].Index + "]的数据有错误");
                    return false;
                }

                String zl_max = (String)row.Cells[grid.Columns["zl_max"].Index].Value;
                if (zl_max != null && Double.TryParse(zl_max, out tmpD))
                {
                    p.zl_max = Double.Parse(zl_max);
                }
                else
                {
                    MessageBox.Show("单元格[" + row.Index + "," + grid.Columns["zl_max"].Index + "]的数据有错误");
                    return false;
                }

                String jg_max = (String)row.Cells[grid.Columns["jg_max"].Index].Value;
                if (jg_max != null && Double.TryParse(jg_max, out tmpD))
                {
                    p.jg_max = Double.Parse(jg_max);
                }
                else
                {
                    MessageBox.Show("单元格[" + row.Index + "," + grid.Columns["jg_max"].Index + "]的数据有错误");
                    return false;
                }

                ps.Add(p);
            }
            return true;
        }

        public ArrayList getConditionStrings()
        {
            //"/search/bggm/?sfCate=TI10&sfLong=3-3&thick=1-1&width=2-2&sfWeight=4-4&mymall=1&time=";
            ArrayList res = new ArrayList();
            StringBuilder sb = new StringBuilder();
            foreach (ProductCondition c in ps)
            {
                sb.Remove(0, sb.Length);

                sb.Append("&itemName=");
                sb.Append(c.flmc.ToString());

                sb.Append("&sfCate=");
                sb.Append(c.fl.ToString());

                sb.Append("&thick=");
                sb.Append(c.hd_min.ToString());
                sb.Append("-");
                sb.Append(c.hd_max.ToString());

                sb.Append("&width=");
                sb.Append(c.kd_min.ToString());
                sb.Append("-");
                sb.Append(c.kd_max.ToString());

                sb.Append("&sfWeight=");
                sb.Append(c.zl_min.ToString());
                sb.Append("-");
                sb.Append(c.zl_max.ToString());

                res.Add(sb.ToString());
            }

            return res;
        }

    }
}
